package com.ruoyi.common.utils.poi.word.render;

import cn.hutool.extra.spring.SpringUtil;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;

/**
 * 批量 word 数据渲染
 */
@Data
public class BatchWordRenderHandler {
    /**
     * 模板枚举
     */
    private List<TemplateEnum> templateEnums = new ArrayList<>();
    /**
     * 业务对象
     */
    private Object object;

    /**
     * 构建批量word 数据渲染对象
     */
    public static BatchWordRenderHandler build(Object object) {
        return new BatchWordRenderHandler().setObject(object);
    }

    /**
     * 增加模板
     */
    public BatchWordRenderHandler add(TemplateEnum templateEnum) {
        if (!templateEnums.contains(templateEnum)) {
            templateEnums.add(templateEnum);
        }
        return this;
    }

    /**
     * 渲染
     */
    public BatchWordRenderHandler render() throws Exception {
        for (TemplateEnum templateEnum : templateEnums) {
            WordRenderHandler handler = (WordRenderHandler) SpringUtil.getBean(templateEnum.getHandle());
            handler.setObject(object);
            handler.init(templateEnum);
            handler.makeData();
            handler.makeSignUser();
            handler.handlePdf();
            handler.buildEnterpriseFile();
        }
        return this;
    }


    /**
     * 渲染
     */
    public WordRenderHandler render(TemplateEnum templateEnum) throws Exception {
        WordRenderHandler handler = (WordRenderHandler) SpringUtil.getBean(templateEnum.getHandle());
        handler.setObject(object);
        handler.init(templateEnum);
        handler.makeData();
        handler.makeSignUser();
        handler.handlePdf();
        handler.buildEnterpriseFile();
        return handler;
    }

    /**
     * 设置业务对象
     */
    private BatchWordRenderHandler setObject(Object object) {
        this.object = object;
        return this;
    }
}
